//
//  BaseObject.m
//  QuWanWang
//
//  Created by tony on 15/4/21.
//  Copyright (c) 2015年 tony. All rights reserved.
//

#import "BaseObject.h"

@implementation BaseObject

//用字典初始化对象
+ (id)objectWithDic:(NSDictionary *)dic;
{
    id object = [[self alloc] initWithDic:dic];
    return object;
}

//用字典初始化对象
- (id)initWithDic:(NSDictionary *)dic
{
    self = [super init];
    if(self)
    {
        @try {
            u_int count;
            
            objc_property_t *properties  =class_copyPropertyList([self class], &count);
            
            NSMutableArray *propertiesArray = [NSMutableArray arrayWithCapacity:count];
            
            for (int i = 0; i < count ; i++)
            {
                const char* propertyName = property_getName(properties[i]);
                [propertiesArray addObject: [NSString stringWithUTF8String: propertyName]];
            }
            
            free(properties);
            
            for(NSInteger i = 0 ; i < propertiesArray.count;i++)
            {
                id value = [dic objectForKey:[propertiesArray objectAtIndex:i]];
                if(value)
                {
                    [self setValue:value forKey:[propertiesArray objectAtIndex:i]];
                }
            }
        }
        @catch (NSException *exception) {
             NSLog(@"parser class %@ error :%@",object_getClass(self),exception.reason);
        }
        @finally {
            
        }
    }
    return self;
}

//用对象初始化对象
- (id)initWithObject:(BaseObject *)obj
{
    self = [super init];
    if(self)
    {
        @try {
            u_int count;
            objc_property_t *properties  = class_copyPropertyList([self class], &count);
            for (int i = 0; i < count ; i++)
            {
                const char* propertyName = property_getName(properties[i]);
                NSString *property = [NSString stringWithUTF8String: propertyName];
                id propertyValue = [obj valueForKey:property];
                if(propertyValue){
                    [self setValue:propertyValue forKey:property];
                }
            }
            free(properties);
        }
        @catch (NSException *exception) {
             NSLog(@"parser class %@ error :%@",object_getClass(self),exception.reason);
        }
        @finally {
            
        }
    }
    return self;
}

//将对象的属性值转化为字典
- (NSDictionary *)getDictionaryByPropertys
{
    NSMutableDictionary *propertyDic = [[NSMutableDictionary alloc] initWithCapacity:1];
    @try {
        u_int count;
        objc_property_t *properties  = class_copyPropertyList([self class], &count);
        for (int i = 0; i < count ; i++)
        {
            const char* propertyName = property_getName(properties[i]);
            NSString *property = [NSString stringWithUTF8String: propertyName];
            id propertyValue = [self valueForKey:property];
            if(propertyValue){
                [propertyDic setObject:propertyValue forKey:property];
            }
        }
        free(properties);
    }
    @catch (NSException *exception) {
        NSLog(@"dictionary class %@ error :%@",object_getClass(self),exception.reason);
    }
    @finally {
        
    }

    return propertyDic;
}

//解档
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if(self = [super init])
    {
        if(aDecoder == nil)return nil;
        u_int count;
        objc_property_t *properties  = class_copyPropertyList([self class], &count);
        for (int i = 0; i < count ; i++)
        {
            const char* propertyName = property_getName(properties[i]);
            NSString *property = [NSString stringWithUTF8String: propertyName];
            id propertyValue = [aDecoder decodeObjectForKey:property];
            //赋值
            [self setValue:propertyValue forKey:property];
        }
        free(properties);
    }
    return self;
}

//归档
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    u_int count;
    objc_property_t *properties  = class_copyPropertyList([self class], &count);
    for (int i = 0; i < count ; i++)
    {
        const char* propertyName = property_getName(properties[i]);
        NSString *property = [NSString stringWithUTF8String: propertyName];
        id propertyValue = [self valueForKey:property];
        [aCoder encodeObject:propertyValue forKey:property];
    }
    free(properties);
}

@end

@implementation NSObject (Utility)
- (NSString *)getClassName
{
    const char *chars = object_getClassName(self);
    NSString *name = [NSString stringWithCString:chars encoding:NSUTF8StringEncoding];
    return name;
}

-(NSString *)getPropertyClassName:(id)obj
{
//    NSString* attrs = [NSString stringWithUTF8String:property_getAttributes(t)];
//    return attrs;
    return nil;
}
@end
